package com.jwthhealth_pub.old.common.Address.ormlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.AndroidConnectionSource;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.support.ConnectionSource;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.old.common.Address.Address;
import com.jwthhealth_pub.old.signutil.db.SQLdm;

import java.io.File;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/5/5.
 */

public class OrmHelper extends OrmLiteSqliteOpenHelper {

    public static final String DB_NAME ="address.db";
    private static final String DATABASE_PATH = "/data/data/com.jwthhealth/databases/";
    public static final int DB_VERSION = 1;
    private Dao<Address, String> addressesDao;

    public Dao<Address, String> getAddressesDao() {
        return addressesDao;
    }

    public OrmHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        boolean dbexist = checkdabase();
    }

    private boolean checkdabase() {
        boolean checkdb = false;

        String myPath = DATABASE_PATH + DB_NAME;
        File dbfile = new File(myPath);
        checkdb = dbfile.exists();

        return checkdb;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            SQLdm sqLdm = new SQLdm();
            sqLiteDatabase = sqLdm.openDatabase(App.mContext, SQLdm.FILEADDRESS);
            AndroidConnectionSource androidConnectionSource = new AndroidConnectionSource(
                    sqLiteDatabase);

            addressesDao = DaoManager.createDao(androidConnectionSource, Address.class);
            List<Address> addresses = addressesDao.queryForAll();
            Log.d("OrmHelper", "addresses:" + addresses);
        } catch (SQLException e) {
            Log.d("OrmHelper", e.toString());
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {

    }

}
